package cn.pcManage.common.service.impl;

import cn.common.common.model.BaseImageInfo;
import cn.common.common.model.ServerResponse;
import cn.pcManage.common.dao.ImageInfoMapper;
import cn.pcManage.common.model.ImageInfo;
import cn.pcManage.common.service.IUtilService;
import net.coobird.thumbnailator.Thumbnailator;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.*;
import java.util.List;
import java.util.UUID;

/**
 * Created by Zhang on 2018/12/11.
 */
@Service
public class UtilServiceImpl implements IUtilService {
    @Autowired
    ImageInfoMapper imageInfoMapper;
    @Override
    public ServerResponse<?> uploadImg(String path, String type, MultipartFile multipartFile) {
        String fileName;
        String fatherType = path;
        try {
            path = "/home/img/" + path + "/";
            //获取上传文件名
            String originalFilename=multipartFile.getOriginalFilename();
            String uploadFileSuffix = originalFilename.substring(
                    originalFilename.lastIndexOf('.'));
            File dir = new File(path);
            if (!dir.exists()) {
                dir.mkdirs();
            }
            fileName = UUID.randomUUID() + uploadFileSuffix;
            FileOutputStream out=new FileOutputStream(new File(path+ fileName));
            byte [] bytes=new byte[10240];
            int len;
            if ("reduce".equals(type)){
                ByteArrayInputStream in = reduceImg(multipartFile.getInputStream());
                while((len=in.read(bytes))!=-1){
                    out.write(bytes, 0, len);
                }
                in.close();
            }else {
                FileInputStream in = (FileInputStream) multipartFile.getInputStream();
                while((len=in.read(bytes))!=-1){
                    out.write(bytes, 0, len);
                }
                in.close();
            }
            out.flush();
            out.close();

        } catch (Exception e) {
            return ServerResponse.createByErrorMessage("上传失败");
        }
        ImageInfo imageInfo = new ImageInfo();
        imageInfo.setFatherId(-1);
        imageInfo.setFatherType(fatherType);
        imageInfo.setImageAddress(path+fileName);
        imageInfo.setImageSize(multipartFile.getSize()+"");
        imageInfoMapper.insertSelective(imageInfo);
        return ServerResponse.createBySuccessMessage(path+fileName);
    }

    @Override
    public void saveImgAddress(Integer fatherId, List<String> imgAddress,String fatherType) {
        for (int i=0;i<imgAddress.size();i++){
            if ("/".equals(imgAddress.get(i).charAt(0))){
                imageInfoMapper.saveImgAddress(fatherId,imgAddress.get(i),i+1);
            }else {
                BaseImageInfo imageInfo = new BaseImageInfo();
                imageInfo.setFatherId(fatherId);
                imageInfo.setFatherType(fatherType);
                imageInfo.setImageAddress(imgAddress.get(i));
                imageInfo.setIndexNum(i+1);
                imageInfoMapper.insertSelective(imageInfo);
            }
        }
    }

    private ByteArrayInputStream reduceImg(InputStream in){
        ByteArrayInputStream is;
        try {
            ByteArrayOutputStream os = new ByteArrayOutputStream();
            Thumbnailator.createThumbnail(in, os, 800, 800);
            is = new ByteArrayInputStream(os.toByteArray());
            in.close();
            os.close();
        } catch (IOException e) {
            return null;
        }
        return is;
    }
}
